<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/7/13
 * Time: 16:17
 */
namespace app\wchat\controller;
use app\repository\wechat\Wechat;
use think\App;
use think\Controller;

class Common extends Controller
{
    public function __construct(App $app = null)
    {
        if(!session('muid')){
            $we = new Wechat();
            $user = $we->W();

            $row = db('admin')->where('openid',$user['openid'])->find();
            if(!$row){
                $this->redirect('Binding/index');
            }
            session('muid',$row['admin_id']);
            session('mgid',$row['group_id']);
        }
        parent::__construct($app);
    }


    public function file(){
        $fileKey = array_keys(request()->file());
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file($fileKey['0']);
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['ext' => 'zip,rar,pdf,swf,ppt,psd,ttf,txt,xls,jpg,png'])->move('uploads');

        if($info){
            $result['code'] = 1;
            $result['info'] = '文件上传成功!';
            $path=str_replace('\\','/',$info->getSaveName());

            $result['url'] = '/uploads/'. $path;
            $result['ext'] = $info->getExtension();
            $result['size'] = byte_format($info->getSize(),2);
            return $result;
        }else{
            // 上传失败获取错误信息
            $result['code'] =0;
            $result['info'] = '文件上传失败!';
            $result['url'] = '';
            return $result;
        }
    }

}